package com.zdsoft.blp.zongbuju;

import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;

import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;
import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.dialog.ShowOrDismissDialog;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.zongbuju.viewpage.CustomViewPager;
import com.zdsoft.blp.zongbuju.viewpage.adapter.VpFmAdapter;
import com.zdsoft.blp.zongbuju.viewpage.fragMent.FenXiangFragment;
import com.zdsoft.blp.zongbuju.viewpage.fragMent.MyFragment;
import com.zdsoft.blp.zongbuju.viewpage.fragMent.ShouYeFragment;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener,IUiListener {
    private CustomViewPager vp_main;
    private int screenWidth;
    private ImageView iv_dh, iv_shouye, iv_fenxiang, iv_wode;
    private TextView tv_shouye, tv_fenxiang, tv_wode;
    private FrameLayout.LayoutParams lp;
    private int currentPage;//当前页面
    private LinearLayout ll_shouye, ll_fenxiang, ll_wode;
    private int green, black;
    private static boolean isExit = false;// 定义一个变量，来标识是否退出
    private ShouYeFragment shouYeFragment;
    private FenXiangFragment fenXiangFragment;
    private MyFragment myFragment;
    @Override
    protected void init() {
        setContentView(R.layout.activity_main);

        //实例化颜色#00cb50
        green = getResources().getColor( R.color.colorPrimaryDark);
        black = getResources().getColor( R.color.black);
        //实例化控件
        vp_main = find(R.id.vp_main);
        iv_dh = find(R.id.iv_dh);
        ll_shouye = find(R.id.ll_shouye);
        ll_fenxiang = find(R.id.ll_fenxiang);
        ll_wode = find(R.id.ll_wode);
        iv_shouye = find(R.id.iv_shouye);
        iv_fenxiang = find(R.id.iv_fenxiang);
        iv_wode = find(R.id.iv_wode);
        tv_shouye = find(R.id.tv_shouye);
        tv_fenxiang = find(R.id.tv_fenxiang);
        tv_wode = find(R.id.tv_wode);

        //添加fragment集合
        List<Fragment> lists = new ArrayList<>();
        shouYeFragment = new ShouYeFragment();
        fenXiangFragment = new FenXiangFragment();
        myFragment = new MyFragment();
        lists.add(shouYeFragment);
        lists.add(fenXiangFragment);
        lists.add(myFragment);
        //实例化动画
        initImage();
        //设置adapter
        vp_main.setAdapter(new VpFmAdapter(getSupportFragmentManager(), lists));
        //设置不让左右滑动
        vp_main.setNoScroll(true);
        vp_main.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (currentPage == 0 && position == 0) {// 0-->1
                    lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 3) + currentPage
                            * (screenWidth / 3));
                } else if (currentPage == 1 && position == 0) {// 1-->0
                    lp.leftMargin = (int) (-(1 - positionOffset)
                            * (screenWidth * 1.0 / 3) + currentPage
                            * (screenWidth / 3));
                } else if (currentPage == 1 && position == 1) {// 1-->2
                    lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 3) + currentPage
                            * (screenWidth / 3));
                } else if (currentPage == 2 && position == 1) {// 2-->1
                    lp.leftMargin = (int) (-(1 - positionOffset)
                            * (screenWidth * 1.0 / 3) + currentPage
                            * (screenWidth / 3));
                }
                iv_dh.setLayoutParams(lp);
            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        //添加监听
        ll_shouye.setOnClickListener(this);
        ll_fenxiang.setOnClickListener(this);
        ll_wode.setOnClickListener(this);

        //初次加载设置首页默认颜色
        clear();
        iv_shouye.setImageResource(R.drawable.sy_hover);
        tv_shouye.setTextColor(green);
    }

    private void initImage() {
        //获取屏幕的参数
        DisplayMetrics metrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        //int height=metrics.heightPixels;//屏幕的高度
        screenWidth = metrics.widthPixels;//获取屏幕的宽
        //获取这条线的参数
        lp = (FrameLayout.LayoutParams) iv_dh.getLayoutParams();
        //给线的宽赋值，屏幕宽的1/4
        lp.width = screenWidth / 3;
        //给线设置改过宽的布局参数
        iv_dh.setLayoutParams(lp);
    }

    @Override
    public void onClick(View v) {
        clear();
        selected(v.getId());
    }

    //清除图片和字体颜色
    private void clear() {
        iv_shouye.setImageResource(R.drawable.sy);
        iv_fenxiang.setImageResource(R.drawable.fx);
        iv_wode.setImageResource(R.drawable.wd);
        tv_wode.setTextColor(black);
        tv_fenxiang.setTextColor(black);
        tv_shouye.setTextColor(black);
    }

    private void selected(int id) {
        ShowOrDismissDialog.show(MainActivity.this);
        switch (id) {
            case R.id.ll_shouye:
                vp_main.setCurrentItem(0);
                iv_shouye.setImageResource(R.drawable.sy_hover);
                tv_shouye.setTextColor(green);
                shouYeFragment.getLunBoInfo();
                break;
            case R.id.ll_fenxiang:
                vp_main.setCurrentItem(1);
                iv_fenxiang.setImageResource(R.drawable.fx_hover);
                tv_fenxiang.setTextColor(green);
                fenXiangFragment.getNet();
                break;
            case R.id.ll_wode:
                vp_main.setCurrentItem(2);
                iv_wode.setImageResource(R.drawable.wd_hover);
                tv_wode.setTextColor(green);
                myFragment.getUserInfo();
                break;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            handler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }


    @Override
    public void onComplete(Object o) {
        Toast.makeText(this,"分享成功",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(UiError uiError) {
        Toast.makeText(this,"分享失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancel() {
        Toast.makeText(this,"取消分享",Toast.LENGTH_SHORT).show();
    }
}


